﻿using System;
using System.Collections.Generic;
using personalplaner.business.common;
using personalplaner.business.stamm;
using personalplaner.common;
using personalplaner.dataprovider;

namespace personalplaner.servicemodel.dataaccess
{
	#region Interface

	public interface IPlanSchemaDataAccess
	{
		#region Get Methods

		/// <summary>
		/// Creates a list of Schemas
		/// </summary>
		/// <param name="showInactive"></param>
		/// <returns></returns>
		IEnumerable<PlanSchema> GetSchemas(bool showInactive);

        /// <summary>
        /// gets a certain schema
        /// </summary>
        /// <param name="schemaid"></param>
        /// <returns></returns>
        PlanSchema GetSchema(int schemaid);

		/// <summary>
		/// Creates a DataView with Schemadata. [schemaname,schemaid,prsschemaid,prsid,abteilungid,rgb,von,bis]
		/// </summary>
		/// <param name="schemaid">planid</param>
		/// <returns>[schemaname,schemaid,prsschemaid,prsid,abteilungid,abteilungname,rgb,von,bis]</returns>
		IEnumerable<PlanSchemaItem> GetPlanSchema(int schemaid);

		/// <summary>
		/// Only brings Personen that are in the schema. [prsID,name,nachname,abteilungid,abteilungname,prsschemaid,rgb]
		/// </summary>
		/// <param name="schemaid">schemaid</param>
		/// <returns>[prsID,name,nachname,abteilungid,abteilungname,prsschemaid,rgb]</returns>
		IEnumerable<PlanSchemaItem> GetPersonenInSchema(int schemaid);

		/// <summary>
		/// Gets a list of all personal contained in the abteilung.[prsID,name,nachname,abteilungid,abteilungname,prsschemaid,rgb]
		/// </summary>
		/// <param name="abteilungid">abteilungid</param>
		/// <returns>[prsID,name,nachname,abteilungid,abteilungname,prsschemaid,rgb]</returns>
		IEnumerable<PlanSchemaItem> GetPersonenInAbteilung(int abteilungid);

        /// <summary>
        /// Gets a list of <see cref="Abteilung"/> that are contained in the schema
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="schemaid">schemaid</param>
        /// <returns>[abteilungid,name]</returns>
        IEnumerable<Abteilung> GetAbteilungenInSchema(DatabaseSettings settings, int schemaid);

        /// <summary>
        /// gets a list of <see cref="ArbeitsPlanHeader"/> that contain the schema
        /// </summary>
        /// <param name="schemaId"></param>
        /// <returns></returns>
        IEnumerable<ArbeitsPlanHeader> GetPlaeneFromSchemaId(int schemaId);

		#endregion

		#region Set Methods

		/// <summary>
		/// [schemaid]
		/// </summary>
		/// <param name="name">name</param>
		/// <returns>[schemaid]</returns>
		int CreateNewSchema(string name);

		/// <summary>
		/// Saves a row in a Planschema
		/// </summary>
		/// <param name="schemaid">schemaid</param>
		/// <param name="schemaitem"></param>
		bool SaveSchemaRow(int schemaid, PlanSchemaItem schemaitem);

		#endregion

		#region Delete Methods

		/// <summary>
		/// Deletes all rows from a schema
		/// </summary>
		/// <param name="schemaid">schemaid</param>
		bool DeleteRowsFromSchema(int schemaid);

        /// <summary>
        /// deletes the schema/layout
        /// </summary>
        /// <param name="schemaId"></param>
        /// <returns></returns>
        bool DeleteSchema(int schemaId);

		#endregion

		#region Update Methods

		/// <summary>
		/// Deactivate = 0; Activate = 1;
		/// </summary>
		/// <param name="schemaid"></param>
		/// <param name="status"></param>
		bool UpdateSchemaStatus(int schemaid, bool status);

		#endregion
	}

	#endregion

	public class PlanSchemaDataAccess : BaseDataAccess, IPlanSchemaDataAccess
	{
		public PlanSchemaDataAccess(ISettingsProvider settings)
            : base(settings)
        {
        }

		#region Get Methods

		/// <summary>
		/// Creates a list of Schemas
		/// </summary>
		/// <param name="showInactive"></param>
		/// <returns></returns>
		public IEnumerable<PlanSchema> GetSchemas(bool showInactive)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanLayoutService.GetSchemas(Settings.DatabaseSettings, Settings.Mandant, showInactive);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return null;
			}
		}

        /// <summary>
        /// gets a certain schema
        /// </summary>
        /// <param name="schemaid"></param>
        /// <returns></returns>
        public PlanSchema GetSchema(int schemaid)
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.PlanLayoutService.GetSchema(Settings.DatabaseSettings, schemaid);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return null;
            }
        }

		/// <summary>
		/// Creates a DataView with Schemadata. [schemaname,schemaid,prsschemaid,prsid,abteilungid,rgb,von,bis]
		/// </summary>
		/// <param name="schemaid">planid</param>
		/// <returns>[schemaname,schemaid,prsschemaid,prsid,abteilungid,abteilungname,rgb,von,bis]</returns>
		public IEnumerable<PlanSchemaItem> GetPlanSchema(int schemaid)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanLayoutService.GetPlanSchema(Settings.DatabaseSettings, schemaid);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return null;
			}
		}

		/// <summary>
		/// Only brings Personen that are in the schema. [prsID,name,nachname,abteilungid,abteilungname,prsschemaid,rgb]
		/// </summary>
		/// <param name="schemaid">schemaid</param>
		/// <returns>[prsID,name,nachname,abteilungid,abteilungname,prsschemaid,rgb]</returns>
		public IEnumerable<PlanSchemaItem> GetPersonenInSchema(int schemaid)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanLayoutService.GetPersonenInSchema(Settings.DatabaseSettings, schemaid);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return null;
			}
		}

		/// <summary>
		/// Gets a list of all personal contained in the abteilung.[prsID,name,nachname,abteilungid,abteilungname,prsschemaid,rgb]
		/// </summary>
		/// <param name="abteilungid">abteilungid</param>
		/// <returns>[prsID,name,nachname,abteilungid,abteilungname,prsschemaid,rgb]</returns>
		public IEnumerable<PlanSchemaItem> GetPersonenInAbteilung(int abteilungid)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanLayoutService.GetPersonenInAbteilung(Settings.DatabaseSettings, abteilungid);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return null;
			}
		}

        /// <summary>
        /// Gets a list of <see cref="Abteilung"/> that are contained in the schema
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="schemaid">schemaid</param>
        /// <returns>[abteilungid,name]</returns>
        public IEnumerable<Abteilung> GetAbteilungenInSchema(DatabaseSettings settings, int schemaid)
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.PlanLayoutService.GetAbteilungenInSchema(Settings.DatabaseSettings, schemaid);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return null;
            }
        }

        /// <summary>
        /// gets a list of <see cref="ArbeitsPlanHeader"/> that contain the schema
        /// </summary>
        /// <param name="schemaId"></param>
        /// <returns></returns>
        public IEnumerable<ArbeitsPlanHeader> GetPlaeneFromSchemaId(int schemaId)
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.PlanService.GetPlaeneFromSchemaId(Settings.DatabaseSettings, schemaId);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return null;
            }
        }

		#endregion

		#region Set Methods

		/// <summary>
		/// [schemaid]
		/// </summary>
		/// <param name="name">name</param>
		/// <returns>[schemaid]</returns>
		public int CreateNewSchema(string name)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanLayoutService.CreateNewSchema(Settings.DatabaseSettings, name, Settings.Mandant);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return 0;
			}
		}

		/// <summary>
		/// Saves a row in a Planschema
		/// </summary>
		/// <param name="schemaid">schemaid</param>
		/// <param name="schemaitem"></param>
		public bool SaveSchemaRow(int schemaid, PlanSchemaItem schemaitem)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanLayoutService.SaveSchemaRow(Settings.DatabaseSettings, schemaid, schemaitem);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return false;
			}
		}

		#endregion

		#region Delete Methods

		/// <summary>
		/// Deletes all rows from a schema
		/// </summary>
		/// <param name="schemaid">schemaid</param>
		public bool DeleteRowsFromSchema(int schemaid)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanLayoutService.DeleteRowsFromSchema(Settings.DatabaseSettings, schemaid);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return false;
			}
		}

        /// <summary>
        /// deletes the schema/layout
        /// </summary>
        /// <param name="schemaId"></param>
        /// <returns></returns>
        public bool DeleteSchema(int schemaId)
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.PlanLayoutService.DeleteSchema(Settings.DatabaseSettings, schemaId);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return false;
            }
        }

		#endregion

		#region Update Methods

		/// <summary>
		/// Deactivate = 0; Activate = 1;
		/// </summary>
		/// <param name="schemaid"></param>
		/// <param name="status"></param>
		public bool UpdateSchemaStatus(int schemaid, bool status)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanLayoutService.UpdateSchemaStatus(Settings.DatabaseSettings, schemaid, status);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return false;
			}
		}

		#endregion
	}
}
